<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"></a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('连接表')">导出</a-button>
      <!-- <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->

      <!-- 高级查询区域 -->
      <!-- <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
       -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"> <a-icon type="delete" />删除 </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>

      <a-button @click="loadData" type="primary" icon="reload">刷新</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="status" slot-scope="text">
          <a-icon type="loading" v-if="text == 'STARTING'" />
          <span v-else :class="text == 'ON' ? 'onStyle' : 'offStyle'"></span>
        </template>
        <template slot="channelType" slot-scope="text">
          <span>{{ FormatchannelType(text) }}</span>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)"
            >下载</a-button
          >
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <span
            v-if="
              record.channelName != 'DCPR信道变频器（主）' &&
                record.channelName != 'DCPR信道变频器（备）' &&
                record.channelName != 'DCPR切换网络' &&
                record.channelName != 'DCPR群解调设备（主）' &&
                record.channelName != 'DCPR群解调设备（备）' &&
                record.channelName != 'DCPR功率放大器' &&
                record.channelName != '数据中转服务器'
            "
          >
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
          </span> -->
          <!-- <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" /> -->
          <!--          <span v-if="record.status == 'OFF'">
            &lt;!&ndash; <a @click="handleChangeChannel(record,1)">开启连接</a> &ndash;&gt;
            <a-popconfirm title="确定开启连接吗?" @confirm="() => handleChangeChannel(record,1)">
              <a>开启连接</a>
            </a-popconfirm>

            <a-divider type="vertical" />
          </span>-->
          <!--          <span v-if="record.status == 'ON'">
            &lt;!&ndash; <a @click="handleChangeChannel(record,0)">停止连接</a> &ndash;&gt;

            <a-popconfirm title="确定停止连接吗?" @confirm="() => handleChangeChannel(record,0)">
              <a>停止连接</a>
            </a-popconfirm>
            <a-divider type="vertical" />
          </span>-->

          <a-dropdown>
            <a class="ant-dropdown-link">
              更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item
                v-if="
                  record.channelName != 'DCPR信道变频器（主）' &&
                    record.channelName != 'DCPR信道变频器（备）' &&
                    record.channelName != 'DCPR切换网络' &&
                    record.channelName != 'DCPR群解调设备（主）' &&
                    record.channelName != 'DCPR群解调设备（备）' &&
                    record.channelName != 'DCPR功率放大器' &&
                    record.channelName != '数据中转服务器'
                "
              >
                <a-popconfirm
                  overlayClassName="poperLayTable"
                  title="确定删除吗?"
                  @confirm="() => handleDelete(record.id)"
                >
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <nmc-channel-info-modal ref="modalForm" @ok="modalFormOk"></nmc-channel-info-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import NmcChannelInfoModal from './modules/NmcChannelInfoModal'
import { httpAction, getAction } from '../../api/manage'

export default {
  name: 'NmcChannelInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    NmcChannelInfoModal
  },
  data() {
    return {
      timer: undefined,
      channelTypeList: [
        { label: 'tcp客户端', value: 'TCP_CLIENT' },
        { label: 'tcp服务端', value: 'TCP_SERVER' },
        { label: '单播', value: 'UDP_UNICAST' },
        { label: '多播', value: 'UDP_MULTICAST' }
      ],
      description: '连接表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '连接名称',
          align: 'center',
          dataIndex: 'channelName',
          sorter: (a,b) => a.channelName.localeCompare(b.channelName)
        },
        {
          title: '连接代码',
          align: 'center',
          dataIndex: 'channelCode'
        },
        {
          title: '连接类型',
          align: 'center',
          dataIndex: 'channelType',
          scopedSlots: { customRender: 'channelType' }
        },
        {
          title: '远程IP',
          align: 'center',
          dataIndex: 'channelHost'
        },
        {
          title: '远程端口',
          align: 'center',
          dataIndex: 'channelPort'
        },
        {
          title: '启动状态', //on:启动 off:关闭
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        // {
        //   title: '0创建时启动，1使用时启动',
        //   align: 'center',
        //   dataIndex: 'lazy',
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/channel/nmcChannelInfo/list',
        delete: '/channel/nmcChannelInfo/delete',
        deleteBatch: '/channel/nmcChannelInfo/deleteBatch',
        exportXlsUrl: '/channel/nmcChannelInfo/exportXls',
        importExcelUrl: 'channel/nmcChannelInfo/importExcel',
        start: `/channel/nmcChannelInfo/start`,
        close: `/channel/nmcChannelInfo/close/`
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  mounted() {},
  destroyed() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleChangeChannel(record, type) {
      let url = ''

      if (type == 1) {
        //停止连接
        url = `${this.url.start}/${record.id}`
      } else if (type == 0) {
        //开启连接
        url = `${this.url.close}/${record.id}`
      }
      httpAction(url, {}, 'post').then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    FormatchannelType(val) {
      let data = this.channelTypeList.find(item => item.value == val)
      return data.label
    },
    initDictConfig() {},
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'channelName', text: '连接名称', dictCode: '' })
      fieldList.push({
        type: 'string',
        value: 'channelHost',
        text: 'tcp_client时对应服务端ip udp_unicast 对应单播地址 udp_mulicast 对应组播地址 tcp_server时 对应本机地址',
        dictCode: ''
      })
      fieldList.push({ type: 'int', value: 'channelPort', text: '对应channel_host', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '启动状态 on:启动 off:关闭', dictCode: '' })
      fieldList.push({ type: 'int', value: 'lazy', text: '0创建时启动，1使用时启动', dictCode: '' })
      this.superFieldList = fieldList
    },
    loadData(arg) {
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records||res.result;
          this.dataSource = this.dataSource.map(item => {
            if(item.channelName == 'dcpr') {
              item.channelName = '数据处理传输服务器'
              item.channelCode = '数据处理传输服务器'
            }
            return item
          })
          this.dataSource.sort((a, b) => b.channelName.localeCompare(a.channelName));
          if(res.result.total)
          {
            this.ipagination.total = res.result.total;
          }else{
            this.ipagination.total = 0;
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
.onStyle,
.offStyle {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: inline-block;
}
.onStyle {
  background: rgb(56, 241, 56);
}
.offStyle {
  background: red;
}
</style>
